//
//  InvitedListCell.swift
//  MT
//
//  Created by 陈悦 on 2020/5/10.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

@objc
protocol InvitedListCellDelegate: NSObjectProtocol {
    
//    @objc optional func homeListCellDidTapCoverImageView(_ cell: HomeListTableViewCell)
 
    @objc optional func invitedListCellDidClickChatButton(_ cell: InvitedListCell, isPlay: Bool)
    
}

class InvitedListCell: UITableViewCell {
    
    var model: MTInviteListModel? {
        didSet {
            if model != nil {
                userHeadIcon.mtSetImage(model?.headerUrl, nil)
                nameLabel.text = model?.nickname
                levelLabel.text = "\(model?.levelName ?? "")"
                genderIcon.image = model?.sex == 0 ? UIImage(named: "sex-man") : UIImage(named: "sex-woman")
                amountLabel.text = "\(model?.rewardTicketAmount ?? 0)"
                if let crn = model?.lastGrowth?.currentLevelName {
                    currentLevel.text = "等级" + crn
                }
                if let nen = model?.lastGrowth?.lastLevelName {
                    nextCurrentLevel.text = "等级" + nen
                }
                
                rewardLab.text = "奖励+\(model?.lastGrowth?.rewardTicketAmount ?? 0)钻"
                amountLabel.text = "\(model?.giftReward?.rewardTicketAmount ?? 0)"
                if model?.lastGrowth?.status == 0 {
                    growStatusLab.isHidden = true
                    currentLevel.textColor = UIColor.text.x1d1a1a
                    nextCurrentLevel.textColor = UIColor.text.x1d1a1a
                    upIcon.image = UIImage(named: "update-black")
                    rewardLab.textColor = UIColor(hex:0xff003d)
                } else {
                    growStatusLab.isHidden = false
                    currentLevel.textColor = UIColor.text.x9b9b9b
                    nextCurrentLevel.textColor = UIColor.text.x9b9b9b
                    upIcon.image = UIImage(named: "update-gray")
                    rewardLab.textColor = UIColor.text.x9b9b9b
                }
                
                taskRewardLab.text = "奖励+\(model?.giftReward?.rewardTicketAmount ?? 0)钻"
                if model?.giftReward?.status == 0 {
                    giftStatusLab.isHidden = true
                    taskDescLab.textColor = UIColor.text.x1d1a1a
                    taskRewardLab.textColor = UIColor(hex:0xff003d)
                } else {
                    giftStatusLab.isHidden = false
                    taskDescLab.textColor = UIColor.text.x9b9b9b
                    taskRewardLab.textColor = UIColor.text.x9b9b9b
                }
                
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    weak var delegate: InvitedListCellDelegate?
    
    lazy var userHeadIcon: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 22
        imageView.clipsToBounds = true
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()

    lazy var nameLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex:0x1d1a1a)
        l.font = PingFangSCSemiboldFont(14)
        l.text = "帅气小伙"
        return l
    }()
    
    lazy var genderIcon: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "sex-man")
        return imageView
    }()
    
    lazy var levelLabel: UILabel = {
        let l = UILabel()
        l.textColor = .white
        l.backgroundColor = UIColor(hex:0xffc000)
        l.layer.cornerRadius = 6
        l.clipsToBounds = true
        l.textAlignment = .center
        l.font = PingFangSCSemiboldFont(10)
        l.text = "M31"
        return l
    }()
    
    lazy var chatBtn: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor(hex:0x426bf2)
        btn.layer.cornerRadius = 4
        btn.setTitle("私信Ta", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = PingFangSCSemiboldFont(12)
        btn.addTarget(self, action: #selector(invitedListCell(didClickChatButton:)), for: .touchUpInside)
        return btn
    }()
    
    lazy var moneyLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex:0x1d1a1a)
        l.font = PingFangSCMediumFont(10)
        l.text = "已获得钻石"
        l.textAlignment = .center
        return l
    }()
    
    lazy var amountLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex:0x1d1a1a)
        l.font = PingFangSCSemiboldFont(16)
        l.text = "40500"
        l.textAlignment = .center
        return l
    }()
    
    lazy var descView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex:0xf6f6f6)
        view.layer.cornerRadius = 4
        return view
    }()
    
    lazy var currentLevel: UILabel = {
        let lab = UILabel()
        lab.text = "等级M407"
        lab.textColor = UIColor(hex:0x9b9b9b)
        lab.font = PingFangSCSemiboldFont(10)
        return lab
    }()
    
    lazy var upIcon: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "update-gray")
        return imageView
    }()
    
    lazy var nextCurrentLevel: UILabel = {
        let lab = UILabel()
        lab.text = "等级M408"
        lab.textColor = UIColor(hex:0x9b9b9b)
        lab.font = PingFangSCSemiboldFont(10)
        return lab
    }()
    
    lazy var rewardLab: UILabel = {
        let lab = UILabel()
        lab.text = "奖励+1000钻"
        lab.textColor = UIColor(hex:0x9b9b9b)
        lab.font = PingFangSCSemiboldFont(10)
        return lab
    }()
    
    lazy var growStatusLab: UILabel = {
        let lab = UILabel()
        lab.attributedText = mtArrtibute(text: "已完成", icon: "pass", position: 1)
        return lab
    }()
    
    lazy var giftStatusLab: UILabel = {
        let lab = UILabel()
        lab.attributedText = mtArrtibute(text: "已完成", icon: "pass", position: 1)
        return lab
    }()
    
    lazy var taskDescLab: UILabel = {
        let lab = UILabel()
        lab.text = "好友二级邀请累计充值/收礼10000钻"
        lab.textColor = UIColor(hex:0x1d1a1a)
        lab.font = PingFangSCSemiboldFont(10)
        return lab
    }()
    
    lazy var taskRewardLab: UILabel = {
        let lab = UILabel()
        lab.text = "奖励+1000钻"
        lab.textColor = UIColor(hex:0xff003d)
        lab.font = PingFangSCSemiboldFont(10)
        return lab
    }()
    
    lazy var spaceView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex:0xf6f6f6)
        return v
    }()
    
    
    class func reuseIdentity() -> String {
        return "InvitedListCellID"
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        contentView.backgroundColor = UIColor.theme.background
        backgroundColor = UIColor.theme.background
    }
    
    private func mtArrtibute(text: String, icon: String, position: Int, iconSize: CGSize? = CGSize(width: 16, height: 16), iconYFix: CGFloat = -5, textColor: UIColor = UIColor(hex:0x9b9b9b)) -> NSAttributedString {
        
        let markAttribute = NSMutableAttributedString(string: text)
        
        let arrtibuteValue: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : textColor,
                                                             NSAttributedString.Key.font : PingFangSCSemiboldFont(10)]
        
        markAttribute.addAttributes(arrtibuteValue, range: NSRange(location: 0, length: text.count))
        
        let markattch = NSTextAttachment()
        
        markattch.image = UIImage(named: icon)
        
        markattch.bounds = CGRect(x: 0, y: iconYFix, width: iconSize!.width, height: iconSize!.height)
        
        let markattchStr = NSAttributedString(attachment: markattch)
        
        /// icon 插入的位置
        if position == 0 {
            markAttribute.insert(markattchStr, at: 0)
        } else {
            markAttribute.append(markattchStr)
        }
        
        return markAttribute
    }
    
    private func setupUI() {
        contentView.addSubview(userHeadIcon)
        contentView.addSubview(nameLabel)
        contentView.addSubview(genderIcon)
        contentView.addSubview(levelLabel)
        contentView.addSubview(chatBtn)
        contentView.addSubview(descView)
        contentView.addSubview(spaceView)
        descView.addSubview(currentLevel)
        descView.addSubview(upIcon)
        descView.addSubview(nextCurrentLevel)
        descView.addSubview(rewardLab)
//        descView.addSubview(growStatusLab)
        descView.addSubview(taskDescLab)
        descView.addSubview(taskRewardLab)
//        descView.addSubview(giftStatusLab)
        
        contentView.addSubview(moneyLabel)
        contentView.addSubview(amountLabel)
    
    }
    
    private func makeConstraint() {
        
        userHeadIcon.snp.makeConstraints { (make) in
            make.left.top.equalTo(10)
            make.width.height.equalTo(44)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(userHeadIcon.snp.right).offset(10)
            make.top.equalTo(14)
        }
        
        levelLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right).offset(10)
            make.centerY.equalTo(nameLabel)
            make.width.equalTo(28)
            make.height.equalTo(12)
        }
        
        genderIcon.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(0)
            make.width.equalTo(33)
            make.height.equalTo(24)
        }
        
        moneyLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(chatBtn.snp.centerY)
            make.right.equalTo(chatBtn.snp.left).offset(-40)
        }
        
        amountLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(moneyLabel)
            make.top.equalTo(moneyLabel.snp.bottom).offset(2)
        }
        
        chatBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-10)
            make.top.equalTo(20)
            make.width.equalTo(60)
            make.height.equalTo(28)
        }
        
        descView.snp.makeConstraints { (make) in
            make.left.equalTo(10)
            make.top.equalTo(userHeadIcon.snp.bottom).offset(10)
            make.right.equalTo(-10)
            make.height.equalTo(50)
        }
        
        currentLevel.snp.makeConstraints { (make) in
            make.left.equalTo(10)
            make.top.equalTo(8)
        }
        
        upIcon.snp.makeConstraints { (make) in
            make.left.equalTo(currentLevel.snp.right).offset(6)
            make.centerY.equalTo(currentLevel)
            make.width.equalTo(38)
            make.height.equalTo(21)
        }
        
        nextCurrentLevel.snp.makeConstraints { (make) in
            make.left.equalTo(upIcon.snp.right).offset(6)
            make.centerY.equalTo(currentLevel)
        }
        
        rewardLab.snp.makeConstraints { (make) in
            make.centerY.equalTo(currentLevel)
        make.left.equalTo(nextCurrentLevel.snp.right).offset(10)
        }
        
//        growStatusLab.snp.makeConstraints { (make) in
//            make.left.equalTo(rewardLab.snp.right).offset(10)
//            make.centerY.equalTo(currentLevel)
//        }
        
        taskDescLab.snp.makeConstraints { (make) in
            make.left.equalTo(currentLevel)
            make.top.equalTo(upIcon.snp.bottom).offset(3)
        }
        
        taskRewardLab.snp.makeConstraints { (make) in
            make.left.equalTo(taskDescLab.snp.right).offset(10)
            make.centerY.equalTo(taskDescLab)
        }
        
//        giftStatusLab.snp.makeConstraints { (make) in
//            make.left.equalTo(taskRewardLab.snp.right).offset(10)
//            make.centerY.equalTo(taskRewardLab)
//        }
        
        spaceView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(5)
        }

    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}

// MARK: ------------------ 点击方法 ----------------------
@objc
extension InvitedListCell {
    /// 点击播放按钮
    private func invitedListCell(didClickChatButton sender: UIButton) {
        if let delegate = delegate {
            delegate.invitedListCellDidClickChatButton?(self, isPlay: true)
        }
    }
}
